<template>
  <div></div>
</template>

<script>
  import { convert } from 'utils/dataType'

  export default {
    props: {
      searchMeta: Array,
    },
    mounted () {
      this.init()
    },
    methods: {
      init () {
        var vm = this;
        var el = $(vm.$el);

        var searchMeta = vm.searchMeta.map(item => ({
          ...item,
          // caption: item.name,
          datatype: convert(item.dataType),
        }))

        searchMeta = WIS_EMAP_SERV.convertModel(searchMeta, 'search')

        searchMeta[0] && (searchMeta[0].quickSearch = true)
        el.bpmAdvancedQuery({
          datamodel: searchMeta
        });

        el.on('search', (e, params) => {
          vm.$emit('search', params)
        })

        el.off("_formChange").on("_formChange", function(){
          el.find('[bh-advanced-query-role="easySearchBtn"]').click();
        });
      },
      /**
       * 获取当前的搜索条件
       * @return {String} 序列化的搜索条件
       */
      getValue () {
        return $(this.$el).bpmAdvancedQuery('getValue');
      },
      /**
       * 搜索条件赋值（只能在高级搜索模式下使用）
       * @param {Object} data 对象数组或者字符串
       */
      setValue (data) {
        $(this.$el).bpmAdvancedQuery('setValue', data);
      },
      /**
       * 清空高级搜索组件中的条件
       */
      clear () {
        $(this.$el).bpmAdvancedQuery('clear');
      },
      dispose () {
        var el = $(this.$el);
        el.off("_formChange");
        el.off('search');
        el.bpmAdvancedQuery('destroy');
      }
    },
    beforeDestroy () {
      this.dispose()
    },
    watch: {
      searchMeta() {
        this.dispose()
        this.init()
      }
    }
  };
</script>
